package com.dykj.apps.web.sysmanager.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.dykj.apps.web.sysmanager.dao.IActDAO;
import com.dykj.apps.web.sysmanager.dao.IAuthManagerDAO;
import com.dykj.apps.web.sysmanager.dao.IGroupDAO;
import com.dykj.apps.web.sysmanager.dao.IUserManagerDAO;
import com.dykj.apps.web.sysmanager.dao.po.SysAct;
import com.dykj.apps.web.sysmanager.dao.po.SysAuth;
import com.dykj.apps.web.sysmanager.dao.po.SysUser;
import com.dykj.apps.web.sysmanager.dto.SysAuthDTO;
import com.dykj.platform.framework.common.paging.Page;
import com.dykj.platform.framework.exception.ApplicationException;
import com.dykj.platform.framework.exception.DatabaseException;
import com.dykj.platform.framework.utils.BeanUtils;

public class AuthManagerBSImpl implements IAuthManagerBS {
	private IAuthManagerDAO dao;
	private IUserManagerDAO userdao;
	private IGroupDAO groupdao;
	private IActDAO actdao;

	public int delAuth(Long key) throws ApplicationException {
		try {
			List user = userdao.queryHaveAuthUser(key);
			if(user.size()>0){
				return -1;
			}else{
				dao.delByKey(SysAuth.class, key);
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return 0;
	}

	public SysAuthDTO getOneSysAuth(Long id) throws ApplicationException {
		SysAuthDTO dto = new SysAuthDTO();
		try {
			Object obj = dao.getObject(SysAuth.class, id);
			if(obj!=null){
				SysAuth auth = (SysAuth)obj;
				BeanUtils.copyProperties(dto, auth);
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return dto;
	}

	public List querySysAuth(SysAuthDTO dto, Page page)	throws ApplicationException {
		List<SysAuthDTO> list = new ArrayList <SysAuthDTO>();
		try {
			List authList = dao.query(dto, page);
			for(int i=0;i<authList.size();i++)
			{
				SysAuthDTO authdto = new SysAuthDTO();
				SysAuth auth = (SysAuth) authList.get(i);
				BeanUtils.copyProperties(authdto , auth);
				list.add(authdto);
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return list;
	}

	public int saveAuth(SysAuthDTO dto, String[] userList ,String[] groupList) throws ApplicationException {
		SysAuth auth = new SysAuth();
		BeanUtils.copyProperties(auth, dto);
		try {
			//dto里roleid ""值用BeanUtils.copyProperties()拷贝到sole里就变0了,所以加了一个 ==0条件
			//if(auth.getAuthid()==null || "".equals(auth.getAuthid())||auth.getAuthid() == 0 ){
				auth.setAuthid(Long.parseLong(dao.getKeyIndex("authid", "sysauth")));
				dto.setAuthid(auth.getAuthid().toString());
			//}
			dao.save(auth);
			//更新sysuser表的authid
			for(int i=0;i<userList.length;i++){
				if(!"".equals(userList[i])){
					SysUser user = (SysUser)userdao.getObject(SysUser.class, userList[i]);
					user.setAuthid(Long.valueOf(dto.getAuthid()));
					userdao.update(user);
				}
			}
		//增加新的sysact行
			for(int i=0;i<groupList.length;i++){
				if(!"".equals(groupList[i])){
					SysAct act = new SysAct();
					act.setGroupid(groupList[i]);
					act.setAuthid(auth.getAuthid());
					act.setActid(Long.parseLong(actdao.getKeyIndex("actid", "sysact")));
					actdao.save(act);
				}
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return 0;
	}
	
	public Map addAuthInit(String type, String groupid, String cauthid) throws ApplicationException {
		List nocheckUser = new ArrayList();
		List nocheckGroup = new ArrayList();
		HashMap<String, List<?>> returnmap = new LinkedHashMap<String, List<?>>();
		try {
			nocheckUser = userdao.queryNoAuthUser(type,groupid);
			//type为0----超管
			if(type.equals("0")){
				nocheckGroup = dao.findAllGroup();			
			}else{
				nocheckGroup = dao.findGroupByAuthid(cauthid);
			}
			
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		returnmap.put("nocheckUser", nocheckUser);
		returnmap.put("nocheckGroup", nocheckGroup);
		return returnmap;
	}
	
	public Map editAuthInit(String authid, String type, String groupid, String cauthid) throws ApplicationException {
		List checkUser = new ArrayList(); //已选用户
		List nocheckUser = new ArrayList(); //可选用户
		List checkGroup = new ArrayList(); //已选机构
		List nocheckGroup = new ArrayList(); //可选机构
		HashMap<String, List<?>> returnmap = new LinkedHashMap<String, List<?>>();
		try{
			checkUser = userdao.queryUserByAuth(authid, type, groupid);
			nocheckUser = userdao.queryNoAuthUser(type, groupid);
			checkGroup = dao.findGroupByAuthid(authid);
			if(type.equals("0")){//超管
				nocheckGroup = dao.findSuperGroupNull(authid);
			}else { //二管
				nocheckGroup = dao.findGroupNull(authid , cauthid);
			}
		}catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		returnmap.put("checkUser", checkUser);
		returnmap.put("nocheckUser", nocheckUser);
		returnmap.put("checkGroup", checkGroup);
		returnmap.put("nocheckGroup", nocheckGroup);
		return returnmap;
	}

	public int updateAuth(SysAuthDTO dto, String[] newUserList, String[] newGroupList,String groupid,String type) throws ApplicationException {
		SysAuth auth = new SysAuth();
		BeanUtils.copyProperties(auth, dto);
		try {
			//保存sysauth表
			dao.update(auth);
			String authid = dto.getAuthid();
			//查询已选的user
			List oldUserList = userdao.queryUserByAuth(authid, type, groupid);
			//将已选的user的authid清空
			for(int i=0;i<oldUserList.size();i++){
			SysUser sysuser = (SysUser) oldUserList.get(i);
			sysuser.setAuthid(null);
			userdao.update(sysuser);
			}
           //更新sysuser表的authid
			for(int i=0;i<newUserList.length;i++){
				if(!"".equals(newUserList[i])){
					SysUser user = (SysUser)userdao.getObject(SysUser.class, newUserList[i]);
					user.setAuthid(Long.valueOf(dto.getAuthid()));
					userdao.update(user);
				}
			}
			//将已有的sysact：group<->authid  清空
			List oldGroupList = actdao.findOldAct(Long.parseLong(authid));
			for(int i = 0; i < oldGroupList.size();i++){
				SysAct sysact = (SysAct) oldGroupList.get(i);
				actdao.delete(sysact);
			}
			//增加新的sysact行
			for(int i=0;i<newGroupList.length;i++){
				if(!"".equals(newGroupList[i])){
					SysAct act = new SysAct();
					act.setGroupid(newGroupList[i]);
					act.setAuthid(auth.getAuthid());
					act.setActid(Long.parseLong(actdao.getKeyIndex("actid", "sysact")));
					actdao.save(act);
				}
			}
			
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return 0;
	}
	public IAuthManagerDAO getDao() {
		return dao;
	}

	public void setDao(IAuthManagerDAO dao) {
		this.dao = dao;
	}

	public IUserManagerDAO getUserdao() {
		return userdao;
	}

	public void setUserdao(IUserManagerDAO userdao) {
		this.userdao = userdao;
	}

	public IGroupDAO getGroupdao() {
		return groupdao;
	}

	public void setGroupdao(IGroupDAO groupdao) {
		this.groupdao = groupdao;
	}

	public IActDAO getActdao() {
		return actdao;
	}

	public void setActdao(IActDAO actdao) {
		this.actdao = actdao;
	}
}
